<template>
    <el-drawer v-model="visible" direction="rtl" :size="1109" :close-on-click-modal="false" title="添加课程章节"  :close-on-press-escape="false" class="mooc-drawer">
        <SearchParams @search="search" ref="searchRef"></SearchParams>
        <!-- 列表 -->
        <table-wrap small hideHeader :total="total" :loading="loading" v-model:pageSize="requestData.pageSize" height="auto" class="list-table"
            v-model:pageIndex="requestData.pageIndex" :tableData="tableData" @request-list="getList" layout="prev, pager, next"
            @row-click="handleCheck">
            <el-table-column fixed="left" min-width="50">
                <template #default="scope">
                    <el-radio v-model="checkCourse.id" :label="scope.row.id" name="tableRadio" @change.native="handleCheck(scope.row)"><span></span></el-radio>
                </template>
            </el-table-column>
            <el-table-column label="序号"  min-width="60" show-overflow-tooltip>
                <template #default="scope">
                    <div class="w60">{{ (requestData.pageIndex - 1) * requestData.pageSize + scope.$index + 1 }}</div>
                </template>
            </el-table-column>
            <el-table-column prop="title" min-width="140" label="课程名称">
                <template #default="scope">
                    <span class="w140" :title="scope.row.title">{{ scope.row.title }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="stageSubjectList" min-width="140" label="学段学科">
                <template #default="scope">
                    <span class="w140" :title="scope.row.stageSubjectName">{{ scope.row.stageSubjectName }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="gradeName" min-width="140" label="年级">
                 <template #default="scope">
                    <span class="w140" :title="scope.row.gradeName">{{ scope.row.gradeName }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="teacherName" min-width="140" label="教师团队">
                <template #default="scope">
                    <span class="w140" :title="scope.row.teacherName">{{ scope.row.teacherName }}</span>
                </template>
            </el-table-column>
            <el-table-column prop="gmtCreate" min-width="130" label="创建时间">
                <template #default="scope">
                    <span>{{ dayjs(scope.row.gmtCreate).format('YYYY.MM.DD HH:mm') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="80" fixed="right" align="center">
                <template #default="scope">
                    <el-button type="primary" link
                        @click="handlePreview(scope.row)">预览</el-button>
                </template>
            </el-table-column>
        </table-wrap>
        <template #footer>
            <div class="footer-container">
                <div class="quick-option">
                    没有所需的课程章节？直接<el-button class="srt-el-button srt-el-button--link srt-el-button-h28 btn" @click="emit('handle-add')">创建课程章节</el-button>
                </div>
                <div>
                    <el-button class="srt-el-button srt-el-button--cancel srt-el-button-h36" @click="cancel">取消</el-button>
                <el-button class="srt-el-button srt-el-button--primary srt-el-button-h36" @click="confirm">确定</el-button>
                </div>
            </div>
        </template>
    </el-drawer>
</template>

<script setup>
import TableWrap from '@/components/table-wrap/table-wrap.vue'
import SearchParams from './search-params.vue'
import { ref, watch, computed, onMounted, nextTick } from 'vue';
import { useRoute } from 'vue-router';
import { useStore } from 'vuex';
import dayjs from 'dayjs';
import { handleStageSubject } from '@/lib/biz-tools.js';

const store = useStore();

const route = useRoute();

const emit = defineEmits(['update:modelValue', 'confirm', 'handle-add']);

const props = defineProps({
    modelValue: {
        type: Boolean,
        default: false
    },
});


const visible = computed({
    get: () => props.modelValue,
    set: val => {
        emit('update:modelValue', val);
    }
})

const searchRef = ref(null);

const tableData = ref([]);

const total = ref(0);

const loading = ref(false);

const checkCourse = ref({});

watch(() => props.modelValue, async(val) => {
    if(!val) {
        checkCourse.value = {};
    }else {
        await nextTick();
        searchRef.value && searchRef.value.clear();
    }
})

const requestData = ref({
    pageIndex: 1,
    pageSize: 10
})



const handleCheck = function(row) {
    checkCourse.value = row;
}

function cancel () {
    emit('update:modelValue', false);
}

function confirm () {
   if(!checkCourse.value.id) {
        G_dispatchEvent('toast', { message: '请选择一个课程章节', type: 'error' });
        return;
   }
   cancel();
   emit('confirm', checkCourse.value);
}

function search(params) {
    checkCourse.value = {};
    requestData.value = Object.assign(requestData.value, params, {pageIndex: 1});
    getList();
}

// 分类列表
function getList () {
    loading.value = true;
    G_transmit('courseMyList', {
        data: {
            bizSource: 'mooc',
            bizSourceId: 0,
            publishState: 1,
            queryScope: 'dtBrand',
            ...requestData.value
        }
    }).then((res) => {
        let list = res.data?.rows || [];
        tableData.value = list.map(item => {
            return {
                ...item,
                gradeName: fmtListArr(item.gradeList, 'gradeName', '暂无'),
                teacherName: fmtListArr(item.lecturerList, 'name', '暂无'),
                stageSubjectName: handleStageSubject(item.stageSubjectList, '暂无'),
            }
        })
        total.value = res.data.total;
    }).catch((_err) => {
        console.log(_err);
    }).finally(() => {
        loading.value = false;
    })
}

// 遍历数组
function fmtListArr(data, paramKey, emptyTxt) {
    if(data?.length) {
       let arr = data.map(item => item[paramKey]);
       return arr.join('、');
    }
    return emptyTxt
}

// 预览
function handlePreview (data) {

    G_go({
        name: 'detailDesign',
        params: {
            courseId: data.id
        }
    }, true);

}

onMounted(async () => {
    // getList();
})

</script>

<style lang="scss">
.list-popper-dialog {
    font-size: 12px !important;
    width: 400px !important;
}
.mooc-drawer .el-drawer__header {
    box-sizing: border-box;
    padding: 16px 20px;
    border-bottom: 1px solid #CCCCCC;
    margin-bottom: 0;
    font-size: 16px;
    line-height: 21px;
    font-weight: bold;
    color: #000000;
    .el-icon svg {
        width: 12px;
        height: 12px;
    }
   
}
.mooc-drawer .el-drawer__footer {
    height: 76px;
    background: #FFFFFF;
    box-shadow: 0px -2px 10px 0px rgba(0,0,0,0.05);
    border-radius: 0px 0px 4px 4px;
    padding: 20px;
}
</style>

<style lang="scss" scoped>
.w140 {
    display: inline-block;
    width: 140px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    vertical-align: middle;
}
.w60 {
    width: 60px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.list-table {
    :deep(.el-table__inner-wrapper::before) {
        display: none;
    }
    :deep(.fr){
         padding-bottom: 10px;
    }

}
.footer-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
}
.quick-option {
    display: flex;
    justify-content: center;
    font-size: 14px;
    color: var(--art-main-text-color);
    line-height: 28px;
    .btn {
        text-decoration: underline;
    }
}
</style>